﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Configuration;
using System.Text;
using System.Diagnostics;
using Newtonsoft.Json.Linq;

public class JsonRequest
{

    private HttpWebRequest request;
    private HttpWebResponse response;
    private Stream resStream;
    private StreamReader reader;

    public JsonRequest(String url)
	{
        request = (HttpWebRequest)WebRequest.Create(url);
	}

    public string getRequest(){

        response = (HttpWebResponse)request.GetResponse();
        string responseFromServer = readResponse(response);
        reader.Close();
        resStream.Close();
        response.Close();

        return responseFromServer;
    }

    public string getRequestWithAuth(string username, string password)
    {
        setBasicAuthHeader(username, password);
        response = (HttpWebResponse)request.GetResponse();
        string responseFromServer = readResponse(response);
        reader.Close();
        resStream.Close();
        response.Close();

        return responseFromServer;
    }




    public bool createUserTeam(string username, string password, string teamName)
    {
        request.Method = "POST";
        string json = "{\"name\": \"" + teamName + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }

    public bool createUserLeague(string username, string password, string leagueName)
    {
        request.Method = "POST";
        string json = "{\"name\": \"" + leagueName + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }

    public bool pickPlayer(string username, string password, string playerID)
    {
        request.Method = "PUT";
        string json = "{\"playerid\": \"" + playerID + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }

    public bool addToWatchlist(string username, string password, string playerID)
    {
        request.Method = "PUT";
        string json = "{\"playerid\": \"" + playerID + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }
    public bool removeFromWatchlist(string username, string password, string playerID)
    {
        request.Method = "DELETE";
        string json = "{\"playerid\": \"" + playerID + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }



    public bool subPlayer(string username, string password, string playerIn, string playerOut)
    {
        request.Method = "PUT";
        string json = "{\"in\": \"" + playerIn + "\", \"out\":\"" + playerOut + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }

    public bool transferPlayer(string username, string password, string playerIn, string playerOut)
    {
        request.Method = "POST";
        string json = "{\"in\": \"" + playerIn + "\", \"out\":\"" + playerOut + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }


    public bool addUserToLeague(string username, string password, string newPlayerUserName)
    {
        request.Method = "PUT";
        string json = "{\"username\": \"" + newPlayerUserName + "\"}";
        setBasicAuthHeader(username, password);
        createMessageBody(json);
        Boolean success = getResponse(202);

        return success;
    }

    public Boolean postRequest(string jsondata, int successCode)
    {

        request.Method = "POST";
        createMessageBody(jsondata);
        Boolean success = getResponse(successCode);
      
        return success;
    }

    public Boolean putRequest(string jsondata, int successCode)
    {
        request.Method = "PUT";
        createMessageBody(jsondata);
        Boolean success = getResponse(successCode);

        return success;
    }



    public Boolean loginRequest(string username, string password)
    {
        setBasicAuthHeader(username, password);
        Boolean success = getResponse(202);

        return success;
    }

    public Boolean changePasswordRequest(string username, string currentPassword, string newPassword)
    {
        request.Method = "PUT";
        string json = "{\"password\": \"" + newPassword + "\"}";
        setBasicAuthHeader(username, currentPassword);
        createMessageBody(json);

        Boolean success = getResponse(204);

        return success;
    }

    private void createMessageBody(string jsondata)
    {
        // Convert JSON data to a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(jsondata);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/json";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
    }

    private Boolean getResponse(int successCode)
    {
        Boolean success;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == successCode)
            {
                success = true;
            }
            else
            {
                Console.WriteLine("Status code: " + response.StatusCode.ToString());
                success = false;
            }
            response.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("Request FAILED");
            Console.WriteLine(e.StackTrace);
            success = false;
        }
        return success;
    }

    private void setBasicAuthHeader(String userName, String userPassword)
    {
        string authInfo = userName + ":" + userPassword;
        authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
        request.Headers["Authorization"] = "Basic " + authInfo;

    }

    private string readResponse(HttpWebResponse response)
    {
        resStream = response.GetResponseStream();
        reader = new StreamReader(resStream);
        return reader.ReadToEnd();
    }



}